package com.wac.blog.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wac.blog.pojo.*;
import com.wac.blog.service.*;
import com.wac.blog.utils.Utils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wac
 * @since 2020-08-04
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserInfoService userInfoService;

    @Autowired
    BlogService blogService;

    @Autowired
    CommentService commentService;

    @Autowired
    QuestionService questionService;

    @Autowired
    UserService userService;


    @RequestMapping("/blog/{uid}")
    public String index(@PathVariable String uid, Model model){
        //信息回填
        UserInfoCallBack(uid,model );
        Page<Blog> page = new Page<>(1,10);
        blogService.page(page, new QueryWrapper<Blog>().eq("author_id", uid).orderByDesc("gmt_create"));
        List<Blog> blogList = page.getRecords();
        model.addAttribute("page",page );
        model.addAttribute("blogList", blogList);
        return "user/index";
    }

    @RequestMapping("/blog/{uid}/{current}")
    public String blogByPage(@PathVariable String uid, @PathVariable Integer current,Model model){
        //信息回填
        UserInfoCallBack(uid,model );
        if (current<1){
            current=1;
        }
        Page<Blog> page = new Page<>(current,10);
        blogService.page(page, new QueryWrapper<Blog>().eq("author_id", uid).orderByDesc("gmt_create"));
        List<Blog> blogList = page.getRecords();
        model.addAttribute("page",page );
        model.addAttribute("blogList", blogList);
        return "user/index";
    }

    @RequestMapping("/question/{uid}/{current}")
    public String questionByPage(@PathVariable String uid, @PathVariable Integer current,Model model){
        //信息回填
        UserInfoCallBack(uid,model );
        if (current<1){
            current=1;
        }
        Page<Question> page = new Page<>(current,10);
        questionService.page(page, new QueryWrapper<Question>().eq("author_id", uid).orderByDesc("gmt_create"));
        List<Question> questionList = page.getRecords();
        model.addAttribute("page",page );
        model.addAttribute("questionList", questionList);
        return "user/user-question";
    }

    @RequestMapping("/comment/{uid}/{current}")
    public String commentByPage(@PathVariable String uid, @PathVariable Integer current,Model model){
        //信息回填
        UserInfoCallBack(uid,model );
        if (current<1){
            current=1;
        }
        Page<Comment> page = new Page<>(current,10);
        commentService.page(page, new QueryWrapper<Comment>().eq("user_id", uid).orderByDesc("gmt_create"));
        List<Comment> commentList = page.getRecords();
        model.addAttribute("page",page );
        model.addAttribute("commentList", commentList);
        return "user/user-comment";
    }

    @RequestMapping("/comment/delete/{uid}/{commentId}")
    public String commentDelete(@PathVariable String uid,@PathVariable String commentId){
        Comment comment = commentService.getOne(new QueryWrapper<Comment>().eq("user_id", uid));
        if (!comment.getUserId().equals(uid)){
            Utils.print("禁止以非作者身份进行删除");
        }
        commentService.removeById(comment);
        return "redirect:/user/comment/"+uid+"/1";
    }

    @RequestMapping("/avatar/{uid}")
    public String toAvator(@PathVariable String uid, Model model){
        UserInfoCallBack(uid,model );
        return "user/update-avatar";
    }

    @PostMapping("/updateavatar")
    public String updateAvator(Model model, HttpServletRequest request){
        String content = request.getParameter("content");
        User user = (User) request.getSession().getAttribute("loginUser");
        String s = content.split("/")[2].split("\\)")[0]; //使用\\转义
        user.setAvatar("/images/upload/"+s);
//        user.setAvatar("D:\\JavaKuangJia\\Javaspringboot\\springboot-blog\\upload\\"+s);
        userService.updateById(user);
        return "redirect:/user/avatar/"+user.getUid();
    }

    @RequestMapping("/question/{uid}")
    public String userQuestion(@PathVariable String uid){
        return "blog/user-question";
    }

    private void UserInfoCallBack(String uid,Model model){
        UserInfo userInfo = userInfoService.getOne(new QueryWrapper<UserInfo>().eq("uid", uid));
        model.addAttribute("userInfo",userInfo );
        if (userInfo.getHobby()!=null && !userInfo.getHobby().equals("")) {
            String[] hobbies = userInfo.getHobby().split(",");
            model.addAttribute("hobbies", hobbies);
        }
        int blogCount = blogService.count(new QueryWrapper<Blog>().eq("author_id", uid));
        int questionCount = questionService.count(new QueryWrapper<Question>().eq("author_id", uid));
        int commentCount = commentService.count(new QueryWrapper<Comment>().eq("user_id", uid));
        model.addAttribute("blogCount", blogCount);
        model.addAttribute("questionCount",questionCount );
        model.addAttribute("commentCount",commentCount );
    }
}

